<?php

namespace app\member\controller\api;

use app\ApiController;
use app\member\model\UsersModel;
use app\member\model\UsersSignModel;


/*------------------------------------------------------ */
//-- 会员相关API
/*------------------------------------------------------ */

class Users extends ApiController
{
    /*------------------------------------------------------ */
    //-- 优先执行
    /*------------------------------------------------------ */
    public function initialize()
    {
        parent::initialize();
        $this->checkLogin();//验证登陆
        $this->Model = new UsersModel();
    }
    /*------------------------------------------------------ */
    //-- 获取登陆会员信息
    /*------------------------------------------------------ */
    public function getInfo()
    {
        $data = $this->userInfo;
        $data['sign_in'] = settings('sign_in');
        $superior = $this->Model->getSuperior($this->userInfo['pid']);
        if ($superior) $data['reg_time'] = date('Y-m-d', $superior['reg_time']);
        $data['superior'] = $superior;
        return $this->success($data);
    }
    /*------------------------------------------------------ */
    //-- 修改会员信息
    /*------------------------------------------------------ */
    public function editInfo()
    {
        $upArr['real_name'] = input('real_name','','trim');
        if (empty($upArr['real_name'])){
            return $this->error('请输入真实姓名.');
        }
        $region_code = input('region_code');
        if (empty($region_code)){
            return $this->error('请选择所在区域.');
        }
        $upArr['nick_name'] = input('nick_name', '', 'trim');
        if (empty($upArr['nick_name']) == true) {
            return $this->error('请填写昵称.');
        }
        $where[] = ['nick_name', '=', $upArr['nick_name']];
        $where[] = ['user_id', '<>', $this->userInfo['user_id']];
        $count = $this->Model->where($where)->count('user_id');
        if ($count > 0) {
            return $this->error('昵称：' . $upArr['nick_name'] . '，已存在.');
        }
        $edit_headimgurl = input('edit_headimgurl',0,'intval');
        if ($edit_headimgurl == 1){
            $headimgurl = input('headimgurl');
            $extend = getFileExtend($headimgurl);
            if ($extend == false){
                return $this->error('未能识别头像图片，请尝试重新上传.');
            }
            $file_path = config('config._upload_') . 'headimg/' . substr($this->userInfo['user_id'], -1) . '/';
            $file_name = random_str(15).rand(10,99).'.'.$extend[1];
            $res = base64ToImage($headimgurl,$file_path,$file_name);
            if ($res == false){
                //return $this->error('头像保存失败，请重试.');
                $upArr['headimgurl'] = $headimgurl;
            }
            else
            {
                $upArr['headimgurl'] = trim($file_path.$file_name, '.');
            }
            
        }
        $upArr['province'] = intval($region_code[0]);
        $upArr['city'] = intval($region_code[1]);
        $upArr['district'] = intval($region_code[2]);
        $upArr['sex'] = input('sex',0,'intval');
        //验证数据是否出现变化
        $dbarr = $this->Model->field(join(',',array_keys($upArr)))->where('user_id',$this->userInfo['user_id'])->find()->toArray();
        $this->checkUpData($dbarr,$upArr,true);

        $res = $this->Model->upInfo($this->userInfo['user_id'], $upArr);
        if ($res < 1) {
            if ($edit_headimgurl == 1){
                @unlink($file_name);
            }
            return $this->error('修改用户信息失败，请重试.');
        }
        if ($edit_headimgurl == 1){
            @unlink($dbarr['headimgurl']);
        }
        $this->_log($this->userInfo['user_id'], '用户修改个人信息.', 'member');
        $this->Model->cleanMemcache($this->userInfo['user_id']);
        return $this->success('修改成功.');
    }
    /*------------------------------------------------------ */
    //-- 修改用户密码
    /*------------------------------------------------------ */
    public function editPwd()
    {
        $res = $this->Model->editPwd(input(), $this);
        if ($res !== true) return $this->error($res);
        $this->Model->logout();
        return $this->success('密码已重置，请用新密码登陆.');
    }
    /*------------------------------------------------------ */
    //-- 修改收款信息
    /*------------------------------------------------------ */
    public function editReceivePay()
    {
        $file_path = config('config._upload_').'receive_pay/';
        $upArr['weixin_usd'] = input('weixin_usd',0,'intval');
        if ($upArr['weixin_usd'] == 1){
            $upArr['weixin_name'] = input('weixin_name','','trim');
            if (empty($upArr['weixin_name'])){
                return $this->error('﻿请输入微信昵称.');
            }
            $upArr['weixin_account'] = input('weixin_account','','trim');
            if (empty($upArr['weixin_account'])){
                return $this->error('﻿请输入微信号.');
            }
            $edit_weixin_qrcode = input('edit_weixin_qrcode',0,'intval');
            if ($edit_weixin_qrcode == 1){
                $weixin_qrcode = input('weixin_qrcode','','trim');
                $upArr['weixin_qrcode'] = copyFile($weixin_qrcode,$file_path);
                if ($upArr['weixin_qrcode'] === false){
                    return $this->error('微信收款二维码处理失败，请重试.');
                }
            }
        }
        $upArr['alipay_usd'] = input('alipay_usd',0,'intval');
        if ($upArr['alipay_usd'] == 1){
            $upArr['alipay_name'] = input('alipay_name','','trim');
            if (empty($upArr['alipay_name'])){
                return $this->error('﻿请输入支付宝帐号姓名.');
            }
            $upArr['alipay_account'] = input('alipay_account','','trim');
            if (empty($upArr['alipay_account'])){
                return $this->error('﻿请输入支付宝帐号.');
            }
            $edit_alipay_qrcode = input('edit_alipay_qrcode',0,'intval');
            if ($edit_alipay_qrcode == 1){
                $alipay_qrcode = input('alipay_qrcode','','trim');
                $upArr['alipay_qrcode'] = copyFile($alipay_qrcode,$file_path);
                if ($upArr['alipay_qrcode'] === false){
                    return $this->error('支付宝收款二维码处理失败，请重试.');
                }
            }
        }
        $upArr['bank_usd'] = input('bank_usd',0,'intval');
        if ($upArr['bank_usd'] == 1){
            $upArr['bank_name'] = input('bank_name','','trim');
            if (empty($upArr['bank_name'])){
                return $this->error('请输入开户银行.');
            }
            $upArr['bank_province'] = input('bank_province',0,'intval');
            $upArr['bank_city'] =  input('bank_city',0,'intval');
            if (empty($upArr['bank_city'])){
                return $this->error('﻿请输入所属省市.');
            }
            $upArr['bank_subbranch'] = input('bank_subbranch','','trim');
            if (empty($upArr['bank_subbranch'])){
                return $this->error('请输入开户支行.');
            }
            $upArr['bank_card_number'] =  input('bank_card_number','','trim');
            if (empty($upArr['bank_card_number'])){
                return $this->error('请输入银行卡号.');
            }
            $upArr['bank_user_name'] =  input('bank_user_name','','trim');
            if (empty($upArr['bank_user_name'])){
                return $this->error('请输入持卡人名.');
            }
            $upArr['bank_idcard_munber'] =  input('bank_idcard_munber','','trim');
            if (empty($upArr['bank_idcard_munber'])){
                return $this->error('请输入身份证号码.');
            }
        }

        //验证数据是否出现变化
        $dbarr = $this->Model->field(join(',',array_keys($upArr)))->where('user_id',$this->userInfo['user_id'])->find()->toArray();
        $this->checkUpData($dbarr,$upArr,true);

        $res = $this->Model->upInfo($this->userInfo['user_id'], $upArr);
        if ($res < 1) {
            if (empty($edit_weixin_qrcode) == false){
                @unlink('.'.$upArr['weixin_qrcode']);
            }
            if (empty($edit_alipay_qrcode) == false){
                @unlink('.'.$upArr['weixin_qrcode']);
            }
            return $this->error('修改用户收款信息失败，请重试.');
        }
        if (empty($edit_weixin_qrcode) == false){
            @unlink('.'.$dbarr['weixin_qrcode']);
            @unlink('.'.$weixin_qrcode);
        }
        if (empty($edit_alipay_qrcode) == false){
            @unlink('.'.$dbarr['alipay_qrcode']);
            @unlink('.'.$alipay_qrcode);
        }
        $this->_log($this->userInfo['user_id'], '用户修改收款信息.', 'member');
        return $this->success('保存成功.');
    }
    /*------------------------------------------------------ */
    //-- 修改用户支付密码
    /*------------------------------------------------------ */
    public function editPayPwd()
    {
        $pay_password = input('password','','trim');
        $old_password = input('old_password','','trim');
        if (empty($pay_password))  return $this->error('请输入新的支付密码.');
        $data['pay_password'] = f_hash($pay_password);
        if (empty($old_password)){
            $res = $this->checkCode('edit_pay_pwd',$this->userInfo['mobile'],input('code'));//验证短信验证
            if ($res !== true){
                return $this->error($res);
            }
        }else{
            $pay_password = $this->Model->where('user_id',$this->userInfo['user_id'])->value('pay_password');
            $old_password = f_hash($old_password);
            if ($old_password != $pay_password){
                return $this->error('旧密码错误.');
            }
            if ($data['pay_password'] == $old_password){
                return $this->error('新密码与旧密码一致，无需修改.');
            }
        }

        $res = $this->Model->where('user_id', $this->userInfo['user_id'])->update($data);
        if ($res < 1) {
            return $this->error('未知错误，处理失败.');
        }
        if (empty($old_password)) {
            $this->_log($this->userInfo['user_id'], '用户通过手机短信修改支付密码.', 'member');
        }else{
            $this->_log($this->userInfo['user_id'], '用户通过旧密码修改支付密码.', 'member');
        }
        return $this->success('支付密码修改成功.');
    }
    //*------------------------------------------------------ */
    //-- 绑定会员手机
    /*------------------------------------------------------ */
    public function bindMobile()
    {
        $res = $this->checkCode('login', input('mobile'), input('code'));//验证短信验证
        if ($res !== true){
            return $this->error($res);
        }
        $res = $this->Model->bindMobile(input(), $this);
        if ($res !== true) return $this->error($res);
        return $this->success('绑定手机成功.');
    }




    //生成水印图
    public function dowaterimg($bgimage,$qrcode,$pathname,$type,$header_img,$user){
        // 已经生成过这个比例的图片就直接返回了
        if (is_file($pathname)){
            return  $pathname ;
        }
        if($type == 1){
            $location1 = \think\Image::WATER_CENTER; //二维码居中
            $location2 =  [50,50];
            $location3 =  [200,140];
            $location4 =  [200,90];
        }elseif($type == 2){
            $location1 = \think\Image::WATER_SOUTH;//二维码下居中
            $location2 =  [380,50];
            $location3 =  [360,250];
            $location4 =  [360,200];
        }else{
            $location1 = [600,700];
            $location2 =  [30,900];
            $location3 =  [180,990];
            $location4 =  [180,940];
        }
        $image = \think\Image::open($bgimage);

        $image->water($qrcode, $location1,100)->save($pathname);
        if($header_img){
            $image->water($header_img, $location2,100)->save($pathname);
        }
        $image->text("昵称 ".$user['nick_name'], "hgzb.ttf", 30,"#000",$location3)->save($pathname);
        $image->text("ID ".$user['user_id'], "hgzb.ttf", 30,"#000",$location4)->save($pathname);
        return $pathname;
    }
    //清空文件夹函数和清空文件夹后删除空文件夹函数的处理
    function deldir($path,$bgimg_name){
        //如果是目录则继续
        if(is_dir($path)){
            //扫描一个文件夹内的所有文件夹和文件并返回数组
            $p = scandir($path);
            foreach($p as $val){
                //排除目录中的.和..
                if($val !="." && $val !=".."){
                    //如果是目录则递归子目录，继续操作
                    if(is_dir($path.$val)){
                        ////子目录中操作删除文件夹和文件
                        //
                        //deldir($path.$val.'/');
                        //
                        ////目录清空后删除空文件夹
                        //
                        //@rmdir($path.$val.'/');
                    }else{
                        //如果是之前生成图片直接删除
                        $code_name = strstr(end(explode('_',$val)), '.', TRUE);
                        if($bgimg_name != $code_name){
                            unlink($path.$val);
                        }
                    }
                }
            }
        }
    }

    /*------------------------------------------------------ */
    //-- 获取签到数据
    /*------------------------------------------------------ */
    public function getSignData(){

        $year = input('year',date('Y')) * 1;
        $month = input('month',date('n')) * 1;

        $data = (new UsersSignModel)->where(['user_id'=>$this->userInfo['user_id'],'year'=>$year,'month'=>$month])->find();
        if(!$data){
            $data['days'] = '';
        }
        $return['code'] = 1;
        $return['data'] = $data;
        return $this->ajaxReturn($return);
    }

    /*------------------------------------------------------ */
    //-- 分享海报二维码
    /*------------------------------------------------------ */
    function getShareImg(){
        $isnew = input('isnew',0,'intval');
        $mun = input('num',0,'intval');
        $is_wxmp = input('is_wxmp',0,'intval');
        $file_name = $this->userInfo['user_id'].'_'.$is_wxmp.'.jpg';
        $file_path = config('config._upload_') . 'my_code/';
        if ($isnew == 0 && file_exists($file_path.$file_name) == true){
            $res['img'] = trim($file_path.$file_name,'.');
            return $this->success($res);
        }

        makeDir($file_path);
        $data['share_avatar'] = $this->Model->getHeadImg($this->userInfo['headimgurl']);
        $data['share_nick_name'] = $this->userInfo['nick_name'];
        $data['share_qrcode'] = $is_wxmp == 0 ? $this->Model->getMyQrCode() : $this->Model->getUserMiniQrcode();

        $data['share_bg'] = settings('share_bg');
        $data['share_bg'] = explode(',',$data['share_bg']);
        $allnum = count($data['share_bg']);
        while ($mun >= $allnum) {
            $mun -= $allnum;
        }
        $data['share_bg'] = $data['share_bg'][$mun];

        $data['share_avatar_xy'] = settings('share_avatar_xy');
        $data['share_avatar_width'] = settings('share_avatar_width');
        $data['share_avatar_shape'] = settings('share_avatar_shape');
        $data['share_nick_name_xy'] = settings('share_nick_name_xy');
        $data['share_nick_name_color'] = settings('share_nick_name_color');
        $data['share_nick_name_center'] = settings('share_nick_name_center');
        $data['share_nick_name_size'] = settings('share_nick_name_size');
        $data['share_qrcode_xy'] = settings('share_qrcode_xy');
        $data['share_qrcode_width'] = settings('share_qrcode_width');
        $MergeImg = new \lib\MergeImg();
        $MergeImg->shareImg($data,$file_path.$file_name);
        $res['img'] = trim($file_path.$file_name,'.').'?v='.time();
        $res['share_url'] = config('config.host_path').'?share_token='.$this->userInfo['token'];
        return $this->success($res);
    }
    /*------------------------------------------------------ */
    //-- 提交验证身份证
    /*------------------------------------------------------ */
    function postIdCard(){
        if ($this->userInfo['check_id_card'] == 1){
            return $this->error('实名已认证，不能修改.');
        }
        if ($this->userInfo['check_id_card'] == 2){
            return $this->error('正在审核中，不能提交.');
        }
        $upArr['id_card_number'] = input('id_card_number','','trim');
        $id_card_positive = input('id_card_positive','','trim');
        $id_card_back = input('id_card_back','','trim');
        if (empty($upArr['id_card_number'])){
            return $this->error('请填写身份证号码.');
        }
        if (empty($id_card_positive)){
            return $this->error('请上传身份证【头像面】.');
        }
        if (empty($id_card_back)){
            return $this->error('请上传身份证【国微面】.');
        }

        $file_path = config('config._upload_').'id_card/';
        $upArr['id_card_positive'] = copyFile($id_card_positive,$file_path);
        if ($upArr['id_card_positive'] === false){
            return $this->error('身份证【头像面】处理失败，请重试.');
        }
        $upArr['id_card_back'] = copyFile($id_card_back,$file_path);
        if ($upArr['id_card_back'] === false){
            return $this->error('身份证【国微面】处理失败，请重试.');
        }
        $upArr['check_id_card'] = 2;
        $res = $this->Model->upInfo($this->userInfo['user_id'],$upArr);
        if ($res < 1){
            @unlink('.'.$upArr['id_card_positive']);
            @unlink('.'.$upArr['id_card_back']);
            return $this->error('提交失败，请重试.');
        }
        @unlink('.'.$id_card_positive);
        @unlink('.'.$id_card_back);
        return $this->success('﻿提交成功，等待审核.');
    }
    // ------------------------------------------
    // -- 生成分享所需参数（调用时需修改此方法）
    // ---------------------------------------------------
    public function getWxShare()
    {
        $url = input('url','','trim');
        $wxShare = (new \app\weixin\model\WeiXinModel)->getSignPackage($url);
        $data['wxShare'] = $wxShare;
        $data['token'] = $this->userInfo['token'];
        return $this->success($data);
    }
}
